{% extends 'extends/nav_front_all_.html' %}

{% block main %}
     <div class="container py-5">
        <!-- Post Management -->
        <div class="card">
            <div class="card-header">
                <h4>帖子管理</h4>
            </div>
            <div class="card-body">
                <!-- Tab Navigation -->
                <ul class="nav nav-tabs" id="postTabs" role="tablist">
                    <li class="nav-item">
                        <a class="nav-link active" id="unchecked-tab" data-bs-toggle="tab" href="#unchecked" role="tab" aria-controls="unchecked" aria-selected="true">未审核</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="passed-tab" data-bs-toggle="tab" href="#passed" role="tab" aria-controls="passed" aria-selected="false">已通过</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" id="rejected-tab" data-bs-toggle="tab" href="#rejected" role="tab" aria-controls="rejected" aria-selected="false">已驳回</a>
                    </li>
                </ul>


                <!-- Tab Content -->
                <div class="tab-content" id="postTabsContent">
                    <!-- Unchecked Posts Tab -->
                    <div class="tab-pane fade show active" id="unchecked" role="tabpanel" aria-labelledby="unchecked-tab">
                        <div class="container mt-4">
                            <div class="row">
                                <div class="col-md-12">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>标题</th>
                                                <th>分类</th>
                                                <th>日期</th>
                                                <th>修改</th>
                                                <th>删除</th>
                                            </tr>
                                        </thead>
                                        <tbody id="unchecked-body">
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Passed Posts Tab -->
                    <div class="tab-pane fade" id="passed" role="tabpanel" aria-labelledby="passed-tab">
                        <div class="container mt-4">
                            <div class="row">
                                <div class="col-md-12">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>标题</th>
                                                <th>分类</th>
                                                <th>日期</th>
                                                <th>删除</th>
                                            </tr>
                                        </thead>
                                        <tbody id="passed-body">
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Rejected Posts Tab -->
                    <div class="tab-pane fade" id="rejected" role="tabpanel" aria-labelledby="rejected-tab">
                        <div class="container mt-4">
                            <div class="row">
                                <div class="col-md-12">
                                    <table class="table table-hover">
                                        <thead>
                                            <tr>
                                                <th>标题</th>
                                                <th>分类</th>
                                                <th>日期</th>
                                                <th>驳回原因</th>
                                            </tr>
                                        </thead>
                                        <tbody id="unpassed-body">
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

     </div>

<script>
    async function DeletePost(url, pid) {
        await Fetch(url, 'delete', {pid: pid}, res => {
            Flash(res.msg)
        })
    }

    function DeleteEvent(url,pid){
        Fetch(url,'delete',{pid:pid},async res => {
            await Flash(res.msg)
            Redirect()
        })
    }


    onUrlsReady(urls=>{
        Fetch(urls.info.myposts,'get',{'checked':false,'passed':false},res=>{
            if(res.status==='success'){
                res.data.blogs.forEach(blog=> {
                    const publish_url=urls.page.front.edit_old.replace('0',blog.pid)
                    const edit_but=`<a class="btn btn-sm btn-primary" href="${publish_url}">修改</a>`
                    const del_but=`<a class="btn btn-sm btn-primary" onclick="DeleteEvent('${urls.service.postdelete}',${blog.pid})">删除</a>`

                    post_url=urls.page.front.blogdetail.replace('0', blog.pid)
                    row=table_item(
                        `<a href="${post_url}">${blog.title}</a>`,
                        `${blog.sort}`,
                        `${blog.time}`,
                        edit_but,
                        del_but
                    )
                    Element_('unchecked-body').insertAdjacentHTML('beforeend', row);
                })
            }
        })

        Fetch(urls.info.myposts,'get',{'checked':true,'passed':true},res=>{
            if(res.status==='success'){
                res.data.blogs.forEach(blog=>{
                    const but=`<a class="btn btn-sm btn-primary" onclick="DeleteEvent('${urls.service.postdelete}',${blog.pid})">删除</a>`

                    post_url=urls.page.front.blogdetail.replace('0', blog.pid)
                    row=table_item(
                        `<a href="${post_url}">${blog.title}</a>`,
                        `${blog.sort}`,
                        `${blog.time}`,
                        but
                    )
                    Element_('passed-body').insertAdjacentHTML('beforeend', row);
                })
            }
        })

        Fetch(urls.info.myposts,'get',{'checked':true,'passed':false},res=>{
            if(res.status==='success'){
                res.data.blogs.forEach(blog=>{
                    post_url=urls.page.front.blogdetail.replace('0', blog.pid)
                    row=table_item(
                        `<a href="${post_url}">${blog.title}</a>`,
                        `${blog.sort}`,
                        `${blog.time}`,
                        `${blog.reason}`
                    )
                    Element_('unpassed-body').insertAdjacentHTML('beforeend', row);
                })
            }
        })
    })
</script>

{% endblock %}